/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Entity.ComplaintSend;
import Entity.InfoLogin;
import Model.EmpProcess;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author h10t93
 */
public class EmpServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String action = request.getParameter("action");
        EmpProcess ep = new EmpProcess();
        HttpSession session = request.getSession();
        if (action.equals("login")) {
            String user = request.getParameter("txtU");
            String pass = request.getParameter("txtP");
            // response.sendRedirect("employeeSendComplaint.jsp");
            if (ep.empLoin(user, pass)) {
                String nameEmp = ep.getInfoLoginUser(user).elementAt(0).getName();
                int idEmp = ep.getInfoLoginUser(user).elementAt(0).getId();
                session.setAttribute("nameEmp", nameEmp);
                session.setAttribute("IDEmp", idEmp);
                response.sendRedirect("employeeSendComplaint.jsp");
            } else {
                response.sendRedirect("employeeLogin.jsp");
            }
        }
        //logout
        if(action.equals("logout")){
        session.removeAttribute("nameEmp");
        session.removeAttribute("IDEmp");
        response.sendRedirect("employeeLogin.jsp");
        }
//        //send complaints
        if (action.equals("sendComp")) {
            if (session.getAttribute("IDEmp") != null) {
                int ide = Integer.parseInt(session.getAttribute("IDEmp").toString());
                String title = request.getParameter("txt_title");
                int cate = Integer.parseInt(request.getParameter("cate"));
                String content = request.getParameter("txt_content");
                ComplaintSend cmps = new ComplaintSend(ide, title, cate, content);
//                cmps.setEmpID(ide);
//                cmps.setTitle(title);
//                cmps.setContent(content);
//                cmps.setCate(cate);
                if (ep.sendComp(cmps)) {
                    int id = Integer.parseInt(session.getAttribute("IDEmp").toString());
                    session.setAttribute("listCompSend", ep.viewComp(id));
                    response.sendRedirect("employeeViewComp.jsp");
                } else {
                    response.sendRedirect("error.jsp");
                }
            } else {
                response.sendRedirect("employeelogin.jsp");
            }
        }
        // view complaint has been send
        if (action.equals("viewCmpSend")) {
            int id = Integer.parseInt(session.getAttribute("IDEmp").toString());
            session.setAttribute("listCompSend", ep.viewComp(id));
            response.sendRedirect("employeeViewComp.jsp");
        }
        // view complaint has been send by status
        if (action.equals("viewCmpBySt")) {
            int id = Integer.parseInt(session.getAttribute("IDEmp").toString());
            int st = Integer.parseInt(request.getParameter("statusComp").toString());
            session.setAttribute("listCompSend",ep.viewCompByStatus(id, st));
            response.sendRedirect("employeeViewComp.jsp");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
